package com.smartao.base.position;

import java.util.List;

import org.hibernate.FetchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.smartao.core.dao.BaseEntityDao;

@Repository
public class PositionDaoImpl extends BaseEntityDao<Position, Long> implements PositionDao {

	@Override
	public List<Position> findByDepartment(Long id) {
		return find(Restrictions.eq("department", id));
	}

	@Override
	public Long countByDepartment(Long id){
		String hql = "SELECT COUNT(*) FROM Position where department.id=?";
		return findUnique(hql, id);
	}

	@Override
	public Position findPositionAndEmployee(Long id) {
		Position position = (Position) getSession().createCriteria(Position.class)
				.add(Restrictions.eq("id", id))
				.setFetchMode("employees", FetchMode.JOIN)
				.uniqueResult();
		return position;
	}
}
